package com.example.mobile2021606145zbq0617homework;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnMainAdd;
    private RecyclerView rvMainPerson;
    List<Person>list = new ArrayList<>();
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;
    String name, vocation;
    int salary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        rvMainPerson.setLayoutManager(new LinearLayoutManager(this));
        rvMainPerson.setAdapter(new RV_Adapter(list));
        openHelper = new MySQLiteOpenHelper(this,"factory.db",null,1);
        database=openHelper.getReadableDatabase();
        Cursor cursor = database.query("staff", null, null, null,null, null, null);
        if (cursor.moveToFirst()){
            do {
                name=cursor.getString(cursor.getColumnIndex("name"));
                vocation=cursor.getString(cursor.getColumnIndex("vocation"));
                salary=cursor.getInt(cursor.getColumnIndex("salary"));
                list.add(new Person(name,vocation,salary));
            }while (cursor.moveToNext());
        }
        cursor.close();
        database.close();

        btnMainAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        btnMainAdd = (Button) findViewById(R.id.btnMainAdd);
        rvMainPerson = (RecyclerView) findViewById(R.id.rvMainPerson);
    }
}